package cn.cosmosx;

import cn.cosmosx.generate.AutoCodeGenerator;
import cn.cosmosx.generate.GeneratorParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Slf4j
@Api(tags = "MybatisPlus-代码生成")
@RestController
@EnableSwagger2
@SpringBootApplication
public class Application {
    @Autowired
    AutoCodeGenerator autoCodeGenerator;

    @Value("${server.port}")
    private int port;
    @Value("${server.servlet.context-path:/}")
    private String rootPath;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @ApiOperation(value = "代码生成", notes = "需要生成代码的表（传入表名，若多张表，则以“,”分隔;若生成全部，则传入“all”）")
    @PostMapping("generate")
    public Object generate(@RequestBody GeneratorParam param) {
        autoCodeGenerator.generate(param);
        return "生成成功";
    }

    /**
     * swagger配置
     * @return
     */
    @Bean
    public Docket commonApi() {
        log.info("swagger访问地址: {}", "http://127.0.0.1:"+port+rootPath+"doc.html");
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(true)
                .apiInfo(new ApiInfoBuilder().title("Generator").description("代码生成器").version("1.0").build())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.cosmosx"))
                .paths(PathSelectors.any())
                .build()
                .groupName("common")
                .pathMapping("/");
    }

}
